let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cstoreId:null,
    commentType:0,
    commentName:'评价类型',
    commentTypeShow:false,
    commentTypeArr:[
      {
        name:'推荐',
        value:3,
      },
      {
        name:'一般',
        value:2,
      },
      {
        name:'不推荐',
        value:1,
      }
    ],
    isLoadMore: true,
    page:1,
    totalPage:0,
    commentList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.storeId??'') !== '') {
      this.setData({
        cstoreId: parseInt(options.storeId)
      })
      this.storeGoodsList();
    }
  },
  assessmentType() {
    this.setData({
      commentTypeShow: !this.data.commentTypeShow
    })
  },
  commentTypeOn(event) {
    let value = event.currentTarget.dataset.value;
    let name = event.currentTarget.dataset.name;
    this.setData({
      commentType: value,
      commentName: name,
      commentTypeShow: false
    })
    this.storeGoodsList();
  },
  storeGoodsList() {
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.commentList");
    let json = {
      cstore_id:this.data.cstoreId,
      page: this.data.page,
      comment_type:this.data.commentType
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          console.log(res);
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            commentList: res.data.data
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.store-manage.commentList");
      let json = {
        store_id:this.data.cstoreId,
        page: this.data.page
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              commentList : this.data.commentList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  previewImage(evt){
    let {images,src} = evt.currentTarget.dataset;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: images // 需要预览的图片http链接列表
    });
  },
    
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
     title: '刷新中...',
   })
   this.setData({
    commentType:0,
    commentName: '评价类型'
   })
   this.storeGoodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})